﻿package view.monsters
{
	/**********************************************************************
	怪物出现在场景的位置：第一波 x = 750；第二波 x = 1200；第三波 x = 1400；
	怪物分布范围：出现位置的前后上下100单位
	**********************************************************************/

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import view.player.*;
	
	public class Monster extends CommonBehave
	{
		public var life:MovieClip;//传入怪物血条
		
		public var StopFrameStart:int;
		public var StopFrameEnd:int;
		public var LeftFrameStart:int;
		public var LeftFrameEnd:int;
		public var RightFrameStart:int;
		public var RightFrameEnd:int;
		public var AttackFrameStart:int;
		public var AttackFrameEnd:int;
		
		public var m_TextLeft:TextField;
		public var thinkTime:int = 10;//怪物攻击时间

		public function Monster()
		{
			// constructor code 
			createTextLeft();
			//m_TextLeft.visible = false;
			//m_TextLeft.x = 0;
			//m_TextLeft.y = -80;
			//Reset();
		}

		public function setRandomY():Number
		{
			var R:Number = Math.random() * 80 + 500;
			return R;
		}
		public function setRandomX():Number
		{
			var R:Number = Math.random() * 80;
			return R;
		}
		public function setPos(pos:int ):void
		{
			this.x = pos + setRandomX();
			this.y = setRandomY();
			m_LastX = this.x;
			m_LastY = this.y;
		}

		//处理逻辑******************
		public function Logic(object:Player):void
		{
			
			switch (m_nState)
			{
				case STATE_STOP :
					FrameUpDate(1,1);//播放帧，美工没拿到，具体帧数未知
					m_TextLeft.visible = false;
					break;
				case STATE_ATTACK :
					FrameUpDate(5,8);//攻击玩家
					if (this.hitTestObject(object.bodyBox))
					{
						object.setState(STATE_BEHIT);
					}
					m_TextLeft.visible = false;
					break;
				case STATE_BEHIT :
					if (Behit(life,object) == true)
					{
						m_TextLeft.text = String(250);
						m_TextLeft.visible = true;
						this.dispatchEvent(new Event("death"));
						this.visible = false;
						//setState(STATE_DEAD);
					}
					break;
				case STATE_DEAD :
					
					break;
			}
			Thinking(object);

		}

		//怪物状态机，距离在attackW,attackH范围攻击，在(attackW，chaseW)，(attackH,chaseH)范围内追逐，否则闲逛
		public function Thinking(object:Player):void
		{
			var attackW:Number = object.width / 6 + 00;
			var attackH:Number = object.height / 6 + 00;
			var chaseW:Number = object.width / 2 + 00;
			var chaseH:Number = object.height / 4 + 00;
			var dir:int;
			if (this.x < object.x)
			{
				this.scaleX = -1;
			}
			else
			{
				this.scaleX = 1;
			}
			if (this.x > object.x - attackW && this.x < object.x + attackW &&
			   this.y > object.y - attackH && this.y < object.y + attackH)
			{
				//这里需要一个间断攻击的判断？？？？？？？
				
				dir = int(1 + 4* Math.random());
				this.m_nMove = dir;
				setState(STATE_MOVE);
				Move();
				thinkTime--;
				if (thinkTime == 0)
				{
					setState(STATE_ATTACK);
					thinkTime = 10;
				}
			}
			else if (this.x > object.x - chaseW && this.x < object.x + chaseW &&
			this.y > object.y - chaseH && this.y < object.y + chaseH)
			{
				if (this.x > object.x)
				{
					m_nMove = STATE_LEFT;
					FrameUpDate(1,2);
					Move();
				}
				else if (this.x < object.x)
				{
					m_nMove = STATE_RIGHT;
					FrameUpDate(1,2);
					Move();
				}
				if (this.y < object.y)
				{
					m_nMove = STATE_DOWN;
					FrameUpDate(1,2);
					Move();
				}
				else if(this.y >　object.y )
				{
					m_nMove = STATE_UP;
					FrameUpDate(1,2);
					Move();
				}
				//setState(STATE_STOP);
			}
			else
			{//在一定范围内闲逛
				dir = int(1 + 4* Math.random());
				this.m_nMove = dir;
				setState(STATE_MOVE);
				Move();
				
			}

		}
		
		public function createTextLeft():void
		{
			m_TextLeft = new TextField();
			m_TextLeft.x = -30;
			m_TextLeft.y = -10;
			m_TextLeft.textColor = 0xFF0000;
			m_TextLeft.thickness = 10;
			this.addChild(m_TextLeft);
			this.setChildIndex(m_TextLeft,0);
		}
		
		

	}

}